﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace XMLInput
{
    public partial class XmlInput : Form
    {
        public XmlInput()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBoxFirstName_Validating(object sender, CancelEventArgs e)
        {
            ValidateName(textBoxFirstName.Text, textBoxFirstName, "Inavlid first name");
        }
        public bool ValidateName(string text, TextBox textBox, string message)
        {
            if (ValidationUtils.IsPersonalNameValid(text))
            {
                errorProvider.SetError(textBox, "");
                return true;
            }
            else
            {
                errorProvider.SetError(textBox, message);
                return false;
            }
        }

        private void textBoxMiddleName_Validating(object sender, CancelEventArgs e)
        {
            ValidateName(textBoxMiddleName.Text, textBoxMiddleName, "Inavlid middle name");
        }

        private void textBoxSurname_Validating(object sender, CancelEventArgs e)
        {
            ValidateName(textBoxSurname.Text, textBoxSurname, "Inavlid surname");
        }

        private void textBoxSsn_Validating(object sender, CancelEventArgs e)
        {
            ValidateSsn(textBoxSsn.Text, textBoxSsn, "Invalid ssn");
        }
        public bool ValidateSsn(string text, TextBox textBox, string message)
        {
            if (ValidationUtils.IsSsnValid(text))
            {
                errorProvider.SetError(textBox, "");
                return true;
            }
            else
            {
                errorProvider.SetError(textBox, message);
                return false;
            }
        }

        private void textBoxPhone_Validating(object sender, CancelEventArgs e)
        {
            ValidatePhone(textBoxPhone.Text, textBoxPhone, "Invalid Phone number!");
        }
        public bool ValidatePhone(string text, TextBox textBox, string message)
        {
            if (ValidationUtils.IsPhoneValid(text))
            {
                errorProvider.SetError(textBox, "");
                return true;
            }
            else
            {
                errorProvider.SetError(textBox, message);
                return false;
            }
        }

        private void textBoxEmail_Validating(object sender, CancelEventArgs e)
        {
            ValidateEmail(textBoxEmail.Text, textBoxEmail, "Invalid Email!");
        }
        public bool ValidateEmail(string text, TextBox textBox, string message)
        {
            if (ValidationUtils.IsEmailValid(text))
            {
                errorProvider.SetError(textBox, "");
                return true;
            }
            else
            {
                errorProvider.SetError(textBox, message);
                return false;
            }
        }

        private void textBoxWebsite_Validating(object sender, CancelEventArgs e)
        {
            ValidateWebSite(textBoxWebsite.Text, textBoxWebsite, "Invalid Website!");
        }
        public bool ValidateWebSite(string text, TextBox textBox, string message)
        {
            if (ValidationUtils.IsWebSiteValid(text))
            {
                errorProvider.SetError(textBox, "");
                return true;
            }
            else
            {
                errorProvider.SetError(textBox, message);
                return false;
            }
        }
        public bool ValidateForm()
        {
            if (!ValidateName(textBoxFirstName.Text, textBoxFirstName, "Inavlid first name") ||
                 ValidateName(textBoxMiddleName.Text, textBoxMiddleName, "Inavlid middle name") ||
                ValidateName(textBoxSurname.Text, textBoxSurname, "Inavlid surname") ||
                ValidateSsn(textBoxSsn.Text, textBoxSsn, "Invalid ssn") ||
                ValidatePhone(textBoxPhone.Text, textBoxPhone, "Invalid Phone number!") ||
                ValidateEmail(textBoxEmail.Text, textBoxEmail, "Invalid Email!") ||
                  ValidateWebSite(textBoxWebsite.Text, textBoxWebsite, "Invalid Website!"))
            {
                return false;
            }
            else
            {
                return true;
            }

        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {

            if (ValidateForm())
            {
                // Close the dialog and return "OK"
                this.DialogResult = DialogResult.OK;
                string fileName = "people.xml";
                Encoding encoding = Encoding.GetEncoding("windows-1251");
                using (XmlTextWriter writer = new XmlTextWriter(fileName, encoding))
                {
                    writer.Formatting = Formatting.Indented;
                    writer.IndentChar = '\t';
                    writer.Indentation = 1;
                    writer.WriteStartDocument();
                    writer.WriteStartElement("people");
                    writer.WriteStartElement("person");
                    writer.WriteStartElement("firstName");
                    writer.WriteString(textBoxFirstName.Text);
                    writer.WriteEndElement();
                    writer.WriteStartElement("middleName");
                    writer.WriteString(textBoxMiddleName.Text);
                    writer.WriteEndElement();
                    writer.WriteStartElement("surname");
                    writer.WriteString(textBoxSurname.Text);
                    writer.WriteEndElement();
                    writer.WriteStartElement("ssn");
                    writer.WriteString(textBoxSsn.Text);
                    writer.WriteEndElement();
                    writer.WriteStartElement("address");
                    writer.WriteString(textBoxAddress.Text);
                    writer.WriteEndElement();
                    writer.WriteStartElement("phone");
                    writer.WriteString(textBoxPhone.Text);
                    writer.WriteEndElement();
                    writer.WriteStartElement("email");
                    writer.WriteString(textBoxEmail.Text);
                    writer.WriteEndElement();
                    writer.WriteStartElement("website");
                    writer.WriteString(textBoxWebsite.Text);
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                }
            }
            else
            {
                StringBuilder allErrors = new StringBuilder();
                foreach (Control control in this.Controls)
                {
                    string errorMsg = errorProvider.GetError(control);
                    if (!String.IsNullOrEmpty(errorMsg))
                    {
                        allErrors.AppendLine(" - " + errorMsg);
                    }
                }

                // Show error message and do not close the dialog
                MessageBox.Show(
                    "Please ented valid values in all fields!\nList of errors:\n" + allErrors,
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
